/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cl.innovision.ui;

import cl.innovision.helper.utils.ListVentanas;
import cl.innovision.ui.render.PanelMapeoRS;
import cl.innovision.ui.render.ProcedureJP;
import cl.innovision.vo.Parameter;
import cl.innovision.vo.Procedure;
import cl.innovision.vo.Procedures;
import cl.innovision.vo.shema.ventanas.Atributos;
import cl.innovision.vo.shema.ventanas.Atributos.Atributo;
import cl.innovision.vo.shema.ventanas.Ventanas.Ventana;
import cl.innovision.vo.shema.ventanas.Ventanas.Ventana.Componentes;
import cl.innovision.vo.shema.ventanas.Ventanas.Ventana.Componentes.Componete;
import cl.innovision.vo.shema.ventanas.Ventanas.Ventana.Componentes.Componete.Eventos.Evento;
import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Patricio
 */
public class PopUpComponente extends javax.swing.JFrame {
    
    
    JPanel panelDianmico=new JPanel();
    /**
     * Creates new form PopUpComponente
     */
    private Ventana ventana=null;
    public PopUpComponente(Ventana ventana) {
        initComponents();
         this.ventana=ventana;
        Procedures procedures =Procedures.getInstance();
        DefaultComboBoxModel dfcbm=new DefaultComboBoxModel();
        for (Procedure p: Procedures.getInstance().getPrecedures())
        {
            dfcbm.addElement(p.getName());
            System.err.println("Servicios: "+p.getName());
        }
        serviciosJCB.setModel(dfcbm);
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tipoMapeoBG = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        tipoComponenteJCB = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        nombreJTF = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        eventoJCB = new javax.swing.JComboBox();
        listaAtributosJSCP = new javax.swing.JScrollPane();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        entradaRB = new javax.swing.JRadioButton();
        salidaRB = new javax.swing.JRadioButton();
        guardarJB = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        mapearJB = new javax.swing.JButton();
        serviciosJCB = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Tipo Componente");

        tipoComponenteJCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "TextInput", "DataGrid", "Button", "ComboBox", "Label", "DateField" }));
        tipoComponenteJCB.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                tipoComponenteJCBItemStateChanged(evt);
            }
        });

        jLabel2.setText("Nombre Componente");

        jLabel3.setText("Evento");

        eventoJCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "Click", "DobleClick" }));

        listaAtributosJSCP.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        listaAtributosJSCP.setAutoscrolls(true);

        jLabel4.setText("Servicio");

        jLabel5.setText("Tipo de Datos");

        tipoMapeoBG.add(entradaRB);
        entradaRB.setText("Entrada");
        entradaRB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                entradaRBActionPerformed(evt);
            }
        });

        tipoMapeoBG.add(salidaRB);
        salidaRB.setText("Salida");

        guardarJB.setText("OK");
        guardarJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarJBActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        mapearJB.setText("Mapear");
        mapearJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mapearJBActionPerformed(evt);
            }
        });

        serviciosJCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(0, 0, Short.MAX_VALUE)
                        .add(guardarJB)
                        .add(18, 18, 18)
                        .add(jButton2)
                        .add(10, 10, 10))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(mapearJB)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(listaAtributosJSCP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 550, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(layout.createSequentialGroup()
                                    .add(jLabel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 103, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(entradaRB)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                    .add(salidaRB))
                                .add(layout.createSequentialGroup()
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(jLabel4)
                                        .add(jLabel3))
                                    .add(66, 66, 66)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                        .add(serviciosJCB, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .add(eventoJCB, 0, 322, Short.MAX_VALUE)))
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                        .add(jLabel1)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .add(tipoComponenteJCB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 320, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                        .add(jLabel2)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(nombreJTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 320, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                        .addContainerGap(18, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(nombreJTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(entradaRB)
                    .add(salidaRB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(tipoComponenteJCB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(serviciosJCB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(eventoJCB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(8, 8, 8)
                .add(mapearJB)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(listaAtributosJSCP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 420, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 9, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(guardarJB)
                    .add(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void entradaRBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_entradaRBActionPerformed
        // TODO add your handling code here:
        
        
    }//GEN-LAST:event_entradaRBActionPerformed

    private void mapearJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mapearJBActionPerformed
        // TODO add your handling code here:
        
         Procedure p=Procedures.getInstance().getProcedureByName(serviciosJCB.getSelectedItem().toString());
         
         if(tipoComponenteJCB.getSelectedItem().toString().equals("DataGrid"))
         {
         try {
         
            // t=null;
            panelDianmico.setLayout(new GridLayout(p.getOutParam().getRs().get(0).getRs().size(),1));

            panelDianmico.removeAll();

            for( Parameter parametro : p.getOutParam().getRs().get(0).getRs())
            {
                ArrayList<Parameter> array=new ArrayList<Parameter>();
                array.add(parametro);
                panelDianmico.add(new PanelMapeoRS("DataGrid",array));
            }
            panelDianmico.setSize(540, 35*p.getOutParam().getRs().get(0).getRs().size());
            //listaProceduresJSCP.getViewport().removeAll();
            listaAtributosJSCP.setViewportView(panelDianmico);
            listaAtributosJSCP.setAutoscrolls(true);
            listaAtributosJSCP.repaint();

            this.repaint();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        }
         else   if(tipoComponenteJCB.getSelectedItem().toString().equals("Label"))
         {
         try {
         
            // t=null;
            panelDianmico.setLayout(new GridLayout(p.getOutParam().getParam().size(),1));

            panelDianmico.removeAll();

            for( Parameter parametro : p.getOutParam().getParam())
            {
                //ProcedureJP pjp=new ProcedureJP(p);
                //pjp.addW
                panelDianmico.add(new PanelMapeoRS("Label",p.getOutParam().getParam()));
            }
            panelDianmico.setSize(540, 35*p.getOutParam().getParam().size());
            //listaProceduresJSCP.getViewport().removeAll();
            listaAtributosJSCP.setViewportView(panelDianmico);
            listaAtributosJSCP.setAutoscrolls(true);
            listaAtributosJSCP.repaint();

            this.repaint();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        }
         else   if(tipoComponenteJCB.getSelectedItem().toString().equals("TextInput"))
         {
         try {
         
            // t=null;
            panelDianmico.setLayout(new GridLayout(p.getInParam().getParam().size(),1));

            panelDianmico.removeAll();

           
                panelDianmico.add(new PanelMapeoRS("TextInput",p.getInParam().getParam()));
            
            panelDianmico.setSize(540, 35*p.getInParam().getParam().size());
            //listaProceduresJSCP.getViewport().removeAll();
            listaAtributosJSCP.setViewportView(panelDianmico);
            listaAtributosJSCP.setAutoscrolls(true);
            listaAtributosJSCP.repaint();

            this.repaint();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        }
          else   if(tipoComponenteJCB.getSelectedItem().toString().equals("DateField"))
         {
         try {
         
            // t=null;
            panelDianmico.setLayout(new GridLayout(p.getInParam().getParam().size(),1));

            panelDianmico.removeAll();

           
                panelDianmico.add(new PanelMapeoRS("DateField",p.getInParam().getParam()));
            
            panelDianmico.setSize(540, 35*p.getInParam().getParam().size());
            //listaProceduresJSCP.getViewport().removeAll();
            listaAtributosJSCP.setViewportView(panelDianmico);
            listaAtributosJSCP.setAutoscrolls(true);
            listaAtributosJSCP.repaint();

            this.repaint();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        }
    }//GEN-LAST:event_mapearJBActionPerformed

    private void guardarJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarJBActionPerformed
        // TODO add your handling code here:
        Componete componete=new Componete();
        componete.setId(nombreJTF.getText());
        componete.setInOut(entradaRB.isSelected()==true?"IN":"OUT");
        componete.setNombreServicio(serviciosJCB.getSelectedItem().toString());
        Evento evento=new Evento();
        evento.setAccion(eventoJCB.getSelectedItem().toString());
        componete.setTipo(tipoComponenteJCB.getSelectedItem().toString());
        Atributos atributos=new Atributos();
       // atributos.
        componete.setAtributos(atributos);
        //Componetes componetes=new Componetes();
        for(int i=0;i<panelDianmico.getComponentCount();i++)
        { 
            Atributo atributo=new Atributo();            
            PanelMapeoRS rs=(PanelMapeoRS) panelDianmico.getComponent(i);
            atributo.setEditable(rs.visibleJCHB.isSelected());
            atributo.setNombre(rs.nombreTxt.getText());
            atributo.setVariable(rs.origenJCB.getSelectedItem().toString());
            atributo.setPrecision(20);
            atributos.getAtributo().add(atributo);
            //atributo.
        }
        addComponente(componete);
        ListVentanas.setVentana(ventana);
        dispose();
        
    }//GEN-LAST:event_guardarJBActionPerformed

    private void tipoComponenteJCBItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_tipoComponenteJCBItemStateChanged
        // TODO add your handling code here:
        if(tipoComponenteJCB.getSelectedItem().toString().equals("TextInput"))
        {
            entradaRB.setEnabled(true);
            salidaRB.setEnabled(true);
            entradaRB.setSelected(true);
        }
        else if(tipoComponenteJCB.getSelectedItem().toString().equals("Label"))
        {
             entradaRB.setEnabled(true);
            salidaRB.setEnabled(true);
            entradaRB.setSelected(true);
        }
         else if(tipoComponenteJCB.getSelectedItem().toString().equals("DataGrid"))
        {
             entradaRB.setEnabled(false);
            salidaRB.setEnabled(true);
            salidaRB.setSelected(true);
        }
         else if(tipoComponenteJCB.getSelectedItem().toString().equals("ComboBox"))
        {
            entradaRB.setEnabled(false);
            salidaRB.setEnabled(true);
            salidaRB.setSelected(true);
        }
                
    }//GEN-LAST:event_tipoComponenteJCBItemStateChanged

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed
    
    public void addComponente(Componete componete)
    {
        if(ventana.getComponentes()==null)
        {
            ventana.setComponentes(new Componentes());
        }
        for (Componete c:ventana.getComponentes().getComponete())
        {
            if(componete.getId().endsWith(c.getId()))
            {
               ventana.getComponentes().getComponete().remove(c);
               ventana.getComponentes().getComponete().add(componete);
               return;
            }
        }
         ventana.getComponentes().getComponete().add(componete);
    }    
       /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PopUpComponente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PopUpComponente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PopUpComponente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PopUpComponente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        /*java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PopUpComponente().setVisible(true);
            }
        });*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton entradaRB;
    private javax.swing.JComboBox eventoJCB;
    private javax.swing.JButton guardarJB;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane listaAtributosJSCP;
    private javax.swing.JButton mapearJB;
    private javax.swing.JTextField nombreJTF;
    private javax.swing.JRadioButton salidaRB;
    private javax.swing.JComboBox serviciosJCB;
    private javax.swing.JComboBox tipoComponenteJCB;
    private javax.swing.ButtonGroup tipoMapeoBG;
    // End of variables declaration//GEN-END:variables
}
